package com.gec.web.servlet;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;

import com.xiaoleilu.hutool.captcha.CaptchaUtil;
import com.xiaoleilu.hutool.captcha.LineCaptcha;
@WebServlet("/imgcode")
public class ImgcodeServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//输入一个顺机的图片
		String imgdir = req.getServletContext().getRealPath("/WEB-INF/imgcodes");
		String[] imgs = new File(imgdir).list();//返回这个文件中所有的文件
		int r = (int)(Math.random()*imgs.length);//随机数0~2
		String img = imgs[r];//获得选中的图片名
		
		String stand = img.replace(".png", "");//把.png去掉.就是答案了10.png
		req.getSession().setAttribute("stand", stand);//把答案记下来,后面要和客户录入核对
		
		//输出图片
		resp.setContentType("image/png");
		OutputStream os = resp.getOutputStream();
		FileUtils.copyFile(new File(imgdir+"/"+img), os);//利用工具类输出
		os.close();
		
//		resp.setContentType("image/png");
//		LineCaptcha ct = CaptchaUtil.createLineCaptcha(200, 100);//创建干扰线的代码
//		ct.createCode();//创建随机数
//		req.getSession().setAttribute("stand", ct.getCode());
//		
//		OutputStream os = resp.getOutputStream();
//		ct.write(os);//利用工具类输出图片
//		os.close();
	}

	
}
